/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.staf.NetworkReportGenerator.V2;

import java.util.Set;
import java.util.HashSet;

import org.snmp4j.smi.VariableBinding;
import org.snmp4j.CommunityTarget;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.TransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.OID;
import org.snmp4j.PDU;
import org.snmp4j.smi.Variable;

import java.io.IOException;

/**
 *
 * @author aezhov
 */
public class NetworkSnmpWorker implements SnmpWorker {
    CommunityTarget target = null;
    Address targetAddress = null;
    TransportMapping tmap = null;
    Snmp snmp = null;


    public NetworkSnmpWorker(String adminip) throws IOException {
        System.out.println("  Connecting to adminip " + adminip);
        targetAddress = GenericAddress.parse("udp:" + adminip + "/161");
        tmap = new DefaultUdpTransportMapping();
        snmp = new Snmp(tmap);
        tmap.listen();
    }

    
    public boolean setSnmpVersion(int version) { return true; }


    public boolean setSnmpCommunity(String community)
    {
        target = new CommunityTarget(targetAddress, new OctetString(community));
        return true;
    }


    public Set<VariableBinding> snmpWalk(String OIDString) throws SnmpException
    {
        System.out.println("Walking snmp for " + OIDString);
        Set<VariableBinding> result = new HashSet<VariableBinding>();
        target.setTimeout(5000);
        target.setRetries(3);
        int version = SnmpConstants.version2c;
        target.setVersion(version);

        OID oid = new OID(OIDString);
        PDU pdu = new PDU();
        pdu.setType(PDU.GETNEXT);
        pdu.add(new VariableBinding(oid));
        boolean finish = false;
        OID lastOID = null;

        System.out.println("  Entering loop");
        while (!finish) {
            ResponseEvent respEvent = null;

            try {
                respEvent = snmp.send(pdu, target, tmap);
            } catch (IOException exc) {
                throw new SnmpException(" snmpWalk: IOException on snmp.send " +
                        exc.toString());
            }

            PDU response = respEvent.getResponse();

            if (response != null) {
                for (int k = 0; k < response.size(); k++) {
                    VariableBinding vb = response.get(k);
                    if (vb.getOid().startsWith(oid)) {
                        if (vb.getOid().equals(lastOID)) {
                            finish = true;
                            break;
                        }

                        lastOID = vb.getOid();
                        result.add(vb);
                        pdu.remove(0);
                        pdu.add(vb);
                    } else {
                        finish = true;
                        break;
                    }
                }
            } else
                break;
        }


        return result;
    }
}
